import { isBlank, isValidNumber } from '@/utils/utilsNew';

export const requiredRule = {
  required: true,
  message: '不能为空',
  trigger: 'blur',
};

// 正数规则
export const positiveNumberRules = {
  validator: (rule, value, callback) => {
    if (!isValidNumber(value)) {
      callback(new Error('必须为数字'));
    } else if (!(value > 0)) {
      callback(new Error('必须大于0'));
    } else if (isBlank(value)) {
      callback(new Error('不能有空格'));
    }
  },
  trigger: 'blur',
};

// 数字规则
export const numberRules = {
  validator: (rule, value, callback) => {
    if (!isValidNumber(value)) {
      callback(new Error('必须为数字'));
    } else if (isBlank(value)) {
      callback(new Error('不能有空格'));
    }
  },
  trigger: 'blur',
};

// 强制校验
export const forceHanlder = (forceConfigs, houDelVO) => {
  return forceConfigs.every(({ key, checkValue, contents }) => {
    if (contents) {
      return contents.every(({ key, checkValue: _check, base }) => {
        if (!base) return true;
        _check = _check || checkValue;
        return _check(houDelVO[key]);
      });
    } else if (key && checkValue) {
      return checkValue(houDelVO[key]);
    }
    return true;
  });
};
